import Vue from 'vue'
import VueRouter from 'vue-router'
import RegisterPage from '@/views/register.vue'
import LoginPage from '@/views/login.vue'
import LayoutPage from '@/views/layout.vue'
// import HomePage from '@/views/home.vue'
// import HousePage from '@/views/house.vue'
// import NewsPage from '@/views/news.vue'
// import MyPage from '@/views/my.vue'
import NotFound from '@/views/NotFound.vue'
import { getToken } from '@/utils/storage'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/register', component: RegisterPage },
  { path: '/login', component: LoginPage },
  {
    path: '/',
    component: LayoutPage,
    children: [
      { path: '/home', component: () => import('@/views/home.vue') },
      { path: '/find', component: () => import('@/views/find.vue') },
      { path: '/news', component: () => import('@/views/news.vue') },
      { path: '/my', component: () => import('@/views/my.vue') }
    ]
  },
  { path: '/search', component: () => import('@/views/search.vue') },
  { path: '/citylist', component: () => import('@/views/city.vue') },
  { path: '/map', component: () => import('@/views/map.vue') },
  { path: '/detail/:id', component: () => import('@/views/details.vue') },
  { path: '*', component: NotFound }
]

const router = new VueRouter({
  routes
})
const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  if (getToken()) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
